package org.jetnuke.modules.blog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogPostDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.BlogCategory;
import org.jetnuke.entities.BlogHits;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.modules.blog.utils.BlogUtils;

public class User implements IAction {

	int id;
	int year=0;
	int month=-1;
	int day=0;
	public void setDay(int day) {
		this.day = day;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		
		if(!BlogUtils.isBlogInitialized(id)){
			
			return new ActionForward(info,"no_blog.vm");
		}
		net.jforum.entities.User usr=DataAccessDriver.getInst().newUserDAO().selectById(id);
		
		
		BlogCategoryDAO bcm=DataAccessDriver.getInst().newBlogCategoryDAO();
		BlogPostDAO bpm=DataAccessDriver.getInst().newBlogPostDAO();
		BlogUserSettingsDAO busm=DataAccessDriver.getInst().newBlogUserSettingsDAO();
		BlogUserSettings bus=busm.retrieveByPk(usr.getId());
		
		List categoryList=bcm.selectByUser(id);
		
		Iterator it=categoryList.iterator();
		
		boolean withDate=false;
		if(year>0 && month>0 && month<13){
			withDate=true;
			if(day==0){
				Calendar cal = new GregorianCalendar(year, month-1, 1);
			    day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 28
			}
		}
		
		List blogList=null;
		int limit=bus.getMAXNUMBERPOST();
		if(limit<1)
			limit=1;
		if(limit>10)
			limit=10;
		
		
		
		
		if(ui!=null && ui.getID()==id){
			vc.put("my_blog",Boolean.TRUE);
		}else{
			BlogHits bh=new BlogHits();
			bh.setAUTHOR_ID(id);
			bh.setPOST_ID(-1);//We are going to see user's blog not a specific blog!
			bh.setTIMESTAMP(new GregorianCalendar().getTime());
			DataAccessDriver.getInst().newBlogHitsDAO().add(bh);
		}
		GregorianCalendar cal=null;
		if(withDate){
			cal = new GregorianCalendar(year, month-1, day,23,59,59);
			blogList=bpm.retrievePublishByUser(id,cal,limit);
		}else{
			cal=new GregorianCalendar();
			blogList=bpm.retrievePublishByUser(id,limit);
		}
		vc.put("blogList",blogList);
		
		HashMap cp=new HashMap();
		vc.put("cp",cp);
		it=categoryList.iterator();
		while(it.hasNext()){
			BlogCategory bc=(BlogCategory)it.next();
			cp.put(new Integer(bc.getID()),bc);
		}
		
		vc.put("settings",bus);
		vc.put("_pagetitle"," - "+bus.getTITLE());
		MainConfig config = (MainConfig) request.getAttribute("MAIN_CONFIG");
		vc.put("nukeUrl",config.getNUKEURL());
		List preLeftBlocks=new ArrayList();
		preLeftBlocks.add(BlogUtils.createUserBlockInfo(vc,usr));
		
		
		preLeftBlocks.add(BlogUtils.createCalendarBlock(vc, cal,id));
		BlogUtils.manageStyle(bus,vc,preLeftBlocks);
		return new ActionForward(op,info);
	}
	public void setId(int id) {
		this.id = id;
	}

}
